import {
  workOderAudit,
  ossDownload
} from '/service/service'
const {
  getCurrentStatusMap
} = require('/utils/statusMap')
Component({
  mixins: [],
  data: {
    mapV2Enable: dd.canIUse('map.optimize'),
    markers: [],
    info: {},
    mapContext: null
  },
  props: {
    id: {
      type: 'string',
      default: ''
    }
  },
  didMount() {
    this.setData({
      mapContext: my.createMapContext('myMap')
    });
    this.getInfo()
  },
  didUpdate() {},
  // TODO 组件切换导致地图标记无法显示的问题
  methods: {
    async getInfo() {
      try {
        const res = await workOderAudit(this.props.id)
        let imgList = []
        if (res.attachments && res.attachments.length > 0) {
          for (const s of res.attachments) {
            const params = {
              ...s,
              ossPath: await ossDownload(s.ossId)
            }
            imgList.push(params)
          }
        }

        // console.log('[图：]', imgList)
        const info = {
          ...res,
          imgList,
          currentStatusName: getCurrentStatusMap(res.status)
        }
        // console.log('[res：]', info)
        this.setData({
          info
        })
        const {
          id: infoId,
          longitude,
          latitude,
          faultLocation,
        } = res
        const markers = [{
          id: infoId,
          longitude,
          latitude,
          title: faultLocation,
          width: "20",
          height: "30",
          iconPath: "/images/mark_point.png",
        }]
        this.setData({
          markers
        })
      } catch (e) {
        my.alert({
          content: e
        })
        console.error(e)
      }
    },
    async imagePreview(e) {
      const {
        url
      } = e.currentTarget.dataset

      this.previewSingleImage(url)
    },
    previewSingleImage(url) {
      dd.previewImage({
        urls: [url],
        current: url,
        success: () => {
          console.log('图片预览成功');
        },
        fail: (err) => {
          console.error('图片预览失败', err);
          dd.showToast({
            content: '预览失败，请重试',
            type: 'fail',
            duration: 2000
          });
        }
      });
    },
    copyData(e) {
      const {
        copy
      } = e.target.dataset
      console.log(copy)
      dd.setClipboard({
        text: copy,
      }).then(() => {
        dd.showToast({
          content: '数据已复制',
          type: 'success'
        })
      });
    }
  },
});